/*   The MIT License
 *   
 *   Carina Engine
 *   Copyright (c) 2009 2010 2011 2012 Zdravko Velinov
 *   
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */

#ifndef _COLLISION_DETECTION_HH
#define _COLLISION_DETECTION_HH

#include "carina/common/global.hh"
#include "carina/common/containers.hh"
#include "carina/common/types.hh"
#include "carina/math.hh"

namespace Carina
{
#undef min
#undef max

struct AABB
{
    Vector3 min;
    Vector3 max;

    AABB() {}
    AABB(const Vector3& _min, const Vector3& _max)
        :   min(_min),
            max(_max) {}

    inline bool testCollision(const AABB& b) const
    {
        return  max.x() >= b.min.x() && min.x() <= b.max.x() &&
                max.y() >= b.min.y() && min.y() <= b.max.y() &&
                max.z() >= b.min.z() && min.z() <= b.max.z();
    }

    bool testIntersection(const Ray& ray) const;
    bool testIntersection(const Segment& seg) const;

    void addPoint(const Vector3& vec);
    float getVolume() const;
    float getSurface() const;

    Vector3 getRadius() const { return (max - min)*0.5f; }
    Vector3 getCenter() const { return (max + min)*0.5f; }

    inline void _resetMax()
    {
        min.set(FLT_MAX, FLT_MAX, FLT_MAX);
        max.set(-FLT_MAX, -FLT_MAX, -FLT_MAX);
    }
};

struct Triangles
{
    size_t      count;
    uint32      *indices;
    Triangles(uint32* ind, size_t c)
        :   count(c),
            indices(ind) {}
};

struct AABBNode
{
    AABB            box;
    bool            is_leaf;
    union
    {
        size_t      second_index;
        Triangles   *triangles;
    };
};

union VertexData
{
    float       *data;
    Vector3     *vec_data;
};

class AABBHierarchy
{
    DynamicArray<AABBNode>  m_Tree;
    DynamicArray<uint32>    m_Indices;
    DynamicArray<Vector3>   m_Data;
public:
    AABBHierarchy();
    ~AABBHierarchy();

    AABBNode& getChild(size_t idx);

    AABBHierarchy(const AABBHierarchy& h);
    AABBHierarchy& operator=(const AABBHierarchy& h);

    bool buildMinSum(VertexData data, size_t data_size, const uint32* indices, size_t indices_size);
//  void buildSplatter(VertexData data, size_t data_size, const uint32* indices, size_t indices_size);

    bool test(const AABBHierarchy& tree) const;
    bool testIntersection(const Ray& ray) const;
    bool testIntersection(const Segment& seg) const;

    void _update(const AABBHierarchy& tree, const Matrix4& matrix);

    void clear();
private:
    bool testGeometry(Triangles* lhs, Triangles* rhs) const;
};

typedef shared_ptr<AABBHierarchy> AABBHierarchyPtr;
}

#endif /* _COLLISION_DETECTION_HH */
